﻿/*----------------------------------------------------------------
//  <copyright file="HttpExtensions.cs" company="MicroCloud@151504200868">
//      Copyright © 2020-2024 MicroCloud Corporation, All rights reserved.
//  </copyright>
//  <site>https://gitee.com/chenmm123/microclouds</site>
//  <last-editor>cmm</last-editor>
//  <last-date>2023-09-25 17:02</last-date>
//----------------------------------------------------------------*/

namespace MicroCloud.AspNetCore
{
    #region "HTTP 扩展方法"
    /// <summary>
    /// HTTP 扩展方法
    /// </summary>
    public static class HttpExtensions
    {
        #region "扩展方法 - HttpRequest"
        #region "读取 <see cref="HttpRequest"/> 的Body为字符串"
        /// <summary>
        /// 读取 <see cref="HttpRequest"/> 的Body为字符串
        /// </summary>
        public static async Task<string> ReadAsStringAsync(this HttpRequest request)
        {
            using StreamReader reader = new(request.Body);
            reader.BaseStream.Seek(0, SeekOrigin.Begin);
            return await reader.ReadToEndAsync();
        }
        #endregion
        #region "设置 <see cref="HttpRequest"/> 的Body为指定字符串"
        /// <summary>
        /// 设置 <see cref="HttpRequest"/> 的Body为指定字符串
        /// </summary>
        /// <param name="request"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static async Task<HttpRequest> WriteBodyAsync(this HttpRequest request, string data)
        {
            if (request.Method == HttpMethod.Get.Method)
            {
                return await Task.FromResult(request);
            }
            byte[] bytes = data.ToBytes();
            request.ContentLength = bytes.Length;
            request.Body = new MemoryStream(bytes);
            return await Task.FromResult(request);
        }
        #endregion

        #endregion

        #region "扩展方法 - HttpResponse"
        #region "读取 <see cref="HttpResponse"/> 的Body为字符串"
        /// <summary>
        /// 读取 <see cref="HttpResponse"/> 的Body为字符串
        /// </summary>
        public static async Task<string> ReadAsStringAsync(this HttpResponse response)
        {
            using StreamReader reader = new(response.Body);
            reader.BaseStream.Seek(0, SeekOrigin.Begin);
            return await reader.ReadToEndAsync();
        }
        #endregion
        #region "设置 <see cref="HttpResponse"/> 的Body为指定字符串"
        /// <summary>
        /// 设置 <see cref="HttpResponse"/> 的Body为指定字符串
        /// </summary>
        /// <param name="response"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static async Task<HttpResponse> WriteBodyAsync(this HttpResponse response, string data)
        {
            if (data == null)
            {
                return await Task.FromResult(response);
            }

            byte[] bytes = data.ToBytes();
            response.ContentLength = bytes.Length;
            response.Body = new MemoryStream(bytes);
            return await Task.FromResult(response);
        }
        #endregion
        #region "获取一个值，该值指示 HTTP 响应是否成功"
        /// <summary>
        /// 获取一个值，该值指示 HTTP 响应是否成功
        /// </summary>
        /// <param name="response"></param>
        /// <returns></returns>
        public static bool IsSuccessStatusCode(this HttpResponse response)
        {
            if (response.StatusCode >= 200)
            {
                return response.StatusCode <= 299;
            }

            return false;
        }
        #endregion

        #endregion

    }
    #endregion

}
